package com.tairan.chapter.flowable.service.impl;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.tairan.chapter.flowable.model.Flow;
import com.tairan.chapter.flowable.service.FlowTaskService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class FlowTaskServiceImpl implements FlowTaskService {

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public PageInfo<Flow> todoList(String userId) {
        List<Flow> flowList = Lists.newArrayList();
        // =============== 已经签收的任务  ===============
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).active()
                .includeProcessVariables().orderByTaskCreateTime().desc().list();
        tasks.forEach(task -> {
            Flow e = new Flow();
            e.setTaskId(task.getId());
            e.setTaskDefKey(task.getTaskDefinitionKey());
            e.setTaskName(task.getName());
            e.setAssignee(task.getAssignee());
            e.setCreateTime(task.getCreateTime());
            e.setExecutionId(task.getExecutionId());
            e.setVars(task.getProcessVariables());

            ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
            e.setProcDefId(pd.getId());
            e.setProcDefname(pd.getName());
            e.setProcDefKey(pd.getKey());
            e.setProcDefversion(pd.getVersion());

            e.setProcInsId(task.getProcessInstanceId());
            e.setStatus("todo");
            flowList.add(e);
        });

        // =============== 等待签收的任务  ===============
        List<Task> toClaimList = taskService.createTaskQuery().taskCandidateUser(userId)
                .includeProcessVariables().active().orderByTaskCreateTime().desc().list();
        toClaimList.forEach(task -> {
            Flow e = new Flow();
            e.setTaskId(task.getId());
            e.setTaskDefKey(task.getTaskDefinitionKey());
            e.setTaskName(task.getName());
            e.setAssignee(task.getAssignee());
            e.setCreateTime(task.getCreateTime());
            e.setExecutionId(task.getExecutionId());
            e.setVars(task.getProcessVariables());

            ProcessDefinition pd = repositoryService.createProcessDefinitionQuery().processDefinitionId(task.getProcessDefinitionId()).singleResult();
            e.setProcDefId(pd.getId());
            e.setProcDefname(pd.getName());
            e.setProcDefKey(pd.getKey());
            e.setProcDefversion(pd.getVersion());

            e.setProcInsId(task.getProcessInstanceId());
            e.setStatus("claim");
            flowList.add(e);
        });
        return new PageInfo<>(flowList);
    }
}
